Repository navigation
Conversation
`RouteUrl` allowed a suffix only after a route path, so adding `?page=2` to a `routeUrl()` result failed the type check. Apply the suffix to a shared base instead, in both the runtime and the generated declaration.
|
Thanks for the fix and the thorough tests. After looking into it, I don't think we should take do this. Here's why: It type-checks URLs that are broken at runtime. `${routeUrl('/a', { query: { a: 1 } })}?b=2`; // /a?a=1?b=2
`${routeUrl('/a', { hash: 'top' })}?b=2`; // /a#top?b=2, so the query ends up in the hashToday the compiler rejects both. It encourages building queries by hand. Appending a query string by hand skips encoding. We already have structured ways to add a query, and they encode values:
It doesn't hold up in large apps. It only helps when the URL is passed inline. It adds to the public types. In the generated It also overlaps with #861 (I'm still working on), which reworks these same declarations. I know this pattern compiled in 1.9.1, and the issue is legit, but this way is probably not recommended for reasons mentioned above. The routes stuff is admittedly a bit messy at the moment (thanks to me). I'm intending to take a step back and rethink how we should do routes in a cleaner fashion but haven't had time to do so. |
|
@yangshun Thank you! That's fair. The scale point is the one I hadn't checked at all. I also didn't realise a type declared inside the generated Still wondering about the docs though. "Checked URL strings" says a typed string must match a route "optionally followed by a query string or hash", and the example right below passes a Would a short paragraph there be welcome, or does #861 change what the docs should say and I should wait for it? Was thinking it covers which forms take a suffix, that a If you'd rather keep these files quiet until #861 lands, happy to sit on it. Just wanted to offer :) |
Fixes #893
Description
RouteUrlaccepted a query or hash suffix only after a route path, so appending?page=2to arouteUrl()result dropped out ofRouteUrl.router.visit(),Link, and props typedRouteUrlrejected it, and Void 1.9.1 accepted the same code.The suffix now applies to a shared base,
BuiltRouteUrl | RoutePathUrl, in the runtime type and in the declarationvoid preparewrites. Both spellings of a route URL take the same suffix, so the union has one place to change.A suffix on a
routeUrl()result that already carriesqueryorhashstill type-checks, as in the issue's${routeUrl('/a', { query: { a: 1 } })}?b=2.BuiltRouteUrlis opaque, so telling that apart needs a second brand. Route path strings already allow the same join.Test
The typed fixture in
route-url-builder.test.tsnow appends a query and a hash to aRouteUrl, to arouteUrl()result, and passes both torouter.visit(). It also pins that a suffix does not skip the route check (/unknown?page=2) and that it needs its own separator.With the same fixture on the base types: